﻿$(function() {
	var intervalCounter = 0;
	var app = angular.module('testCaseApp', [ 'toaster', 'ngAnimate' ]);
	app
			.controller(
					'testCaseCtrl',
					function($scope, $http, $filter, toaster) {
						$scope.firstPage = 1;
						$scope.pageNum = 5;
						$scope.page = 1;
						// 默认下方分页不显示。
						$scope.isShowPage = false;
						// 加载页面
						$scope.load_page = function(page) {
							if (page < 1) {
								toaster.pop({
									type : 'error',
									title : '提示',
									body : "已经是第一页了",
									timeout : 2000
								});
							} else if (page > $scope.pageCount) {
								toaster.pop({
									type : 'error',
									title : '提示',
									body : "已经是最后一页了",
									timeout : 2000
								});
							} else {
								$scope.p_current = page;
								$scope.sub(page);
								$scope.testCases = [];
								for (var a = (page - 1) * 10; a < (page) * 10; a++) {
									if (a < $scope.resultAllLength) {
										$scope.testCases
												.push($scope.testCasesAll[a]);
									}
								}
								$scope.beginAndEnd($scope.p_current);
							}
						}
						// 点击翻页
						$scope.sub = function(page) {
							if (page >= $scope.pageNum) {
								$scope.firstPage = page
										- Math.floor($scope.pageNum / 2);
							} else {
								$scope.firstPage = 1;
							}
							if ($scope.firstPage > $scope.pageCount
									- $scope.pageNum) {
								$scope.firstPage = $scope.pageCount
										- $scope.pageNum + 1;
							}
							$scope.pages = setPage($scope.pageCount,
									$scope.pageNum, $scope.firstPage);
							$scope.page = page;
						}
						// 一键整合用例成功后的弹出框关闭按钮和复制按钮
						$scope.oneKeyTestCaseClose = function() {
							$("#spm").hide();
							$(".oneKeyTestCase").hide();
						}
						$scope.copyTestCase = function() {
							$(".copyText").focus();
							$(".copyText").select();
							toaster.pop({
								type : 'success',
								title : '提示',
								body : "请按CTRL+C或者右键点击复制",
								timeout : 2000
							});
						}
						// 自动过滤并整合用例，一键整合用例。
						$scope.oneKey = function() {
							$scope.testCasesOneKey = $filter("filter")(
									$scope.testCases, $scope.selectKeyword,
									false);
							if ($scope.testCasesOneKey == undefined
									|| $scope.testCasesOneKey.length == 0) {
								toaster.pop({
									type : 'error',
									title : '提示',
									body : "没有要整合的东西哦",
									timeout : 2000
								});
							} else {
								var msg = "";
								for (var i = 0; i < $scope.testCasesOneKey.length; i++) {
									var remark = $scope.testCasesOneKey[i].remark == null ? ""
											: $scope.testCasesOneKey[i].remark;
									msg = msg
											+ "----------------用例"
											+ (i + 1)
											+ "----------------"
											+ "\n功能名称："
											+ $scope.testCasesOneKey[i].funchildname
											+ "\n操作步骤："
											+ $scope.testCasesOneKey[i].step
											+ "\n预期结果："
											+ $scope.testCasesOneKey[i].expectresult
											+ "\n备注：" + remark + "\n";
								}

								$("#spm").show();
								var target = $(".oneKeyTestCase");
								var targetWidth = target.outerWidth();
								var targetHeight = target.outerHeight();
								if (isIE(6)) {
									$("#spm").hide();
									// $("select").hide();
									var top = $(document).scrollTop()
											+ $(window).height() / 2;
									target.css({
										"top" : top + "px"
									});
									$(window).scroll(
											function() {
												var top = $(document)
														.scrollTop()
														+ $(window).height()
														/ 2;
												target.css({
													"top" : top + "px"
												});
											});
								}
								target.css({
									"margin-top" : -parseInt(targetHeight / 2)
											+ "px",
									"margin-left" : -parseInt(targetWidth / 2)
											+ "px"
								});
								target.show();
								$scope.testCaseMsg = msg;
							}
						}
						// 删除用例
						$scope.deleteCase = function() {
							if (signGroupAll.length > 0) {

								$http(
										{
											method : 'POST',
											url : "testCase/deleteCase.do",
											headers : {
												'Content-Type' : 'application/x-www-form-urlencoded'
											},
											params : {
												signGroup : encodeURI(signGroup)
											},
										})
										.success(
												function(data) {// 这里的data，就是后台传递过来的数据
													if (data.tag == "success") {
														toaster.pop({
															type : 'success',
															title : '提示',
															body : "删除用例成功",
															timeout : 2000
														});
														// 根据选择，循环删除这几个用例
														for (var i = 0; i < signGroup.length; i++) {
															$scope.testCases
																	.splice(
																			$scope.testCases
																					.indexOf(signGroupAll[i]),
																			1);
														}
														signGroup = [];
														signGroupAll = [];
													} else {
														toaster.pop({
															type : 'error',
															title : '提示',
															body : "删除用例失败",
															timeout : 2000
														});

													}
												}).error(
												function(data, status, headers,
														config) {
													toaster.pop({
														type : 'error',
														title : '提示',
														body : "请求出错了",
														timeout : 2000
													});

												});
							} else {
								toaster.pop({
									type : 'error',
									title : '提示',
									body : "请选择至少一个用例",
									timeout : 2000
								});
							}
						}
						// 是否显示选择框
						$scope.isShowSelect = function(x) {
							if (x == '公共') {
								return false;
							}
							return true;
						}
						// 编辑用例，提交
						$scope.EditCase = function() {
							if ($scope.sortEdit == undefined
									|| $scope.sortEdit == 0) {
								toaster.pop({
									type : 'error',
									title : '提示',
									body : "用例分类不能为空",
									timeout : 2000
								});
							} else if ($scope.funcEdit == undefined
									|| $scope.funcEdit == null) {
								toaster.pop({
									type : 'error',
									title : '提示',
									body : "功能概括不能为空",
									timeout : 2000
								});
							} else if ($scope.funchildnameEdit == undefined
									|| $scope.funchildnameEdit == null
									|| $scope.funchildnameEdit.trim() == '') {
								toaster.pop({
									type : 'error',
									title : '提示',
									body : "功能名称不能为空",
									timeout : 2000
								});
							} else if ($scope.stepEdit == undefined
									|| $scope.stepEdit == null
									|| $scope.stepEdit.trim() == '') {
								toaster.pop({
									type : 'error',
									title : '提示',
									body : "操作步骤不能为空",
									timeout : 2000
								});
							} else if ($scope.expectresultEdit == undefined
									|| $scope.expectresultEdit == null
									|| $scope.expectresultEdit.trim() == '') {
								toaster.pop({
									type : 'error',
									title : '提示',
									body : "预期结果不能为空",
									timeout : 2000
								});
							} else {
								$http(
										{
											method : 'POST',
											url : "testCase/EditCase.do",
											headers : {
												'Content-Type' : 'application/x-www-form-urlencoded'
											},
											params : {
												editCaseId : $scope.editCaseId,
												sortEdit : $scope.sortEdit,
												funcEdit : $scope.funcEdit,
												funchildnameEdit : encodeURI($scope.funchildnameEdit),
												stepEdit : encodeURI($scope.stepEdit),
												expectresultEdit : encodeURI($scope.expectresultEdit),
												remarkEdit : $scope.remarkEdit == undefined ? ""
														: encodeURI($scope.remarkEdit)
											},
										}).success(function(data) {// 这里的data，就是后台传递过来的数据
									if (data.tag == "success") {
										toaster.pop({
											type : 'success',
											title : '提示',
											body : "编辑用例成功",
											timeout : 2000
										});
										$("#spm").hide();
										$(".PopupEdit").hide(100);
									} else {
										toaster.pop({
											type : 'error',
											title : '提示',
											body : "编辑用例失败",
											timeout : 2000
										});

									}
								})
										.error(
												function(data, status, headers,
														config) {
													toaster.pop({
														type : 'error',
														title : '提示',
														body : "请求出错了",
														timeout : 2000
													});

												});
							}
						}
						// 编辑用例，弹出框
						$scope.editTestCase = function(x) {
							$("#spm").show();
							var target = $(".PopupEdit");
							var targetWidth = target.outerWidth();
							var targetHeight = target.outerHeight();
							if (isIE(6)) {
								$("#spm").hide();
								// $("select").hide();
								var top = $(document).scrollTop()
										+ $(window).height() / 2;
								target.css({
									"top" : top + "px"
								});
								$(window).scroll(
										function() {
											var top = $(document).scrollTop()
													+ $(window).height() / 2;
											target.css({
												"top" : top + "px"
											});
										});
							}
							target.css({
								"margin-top" : -parseInt(targetHeight / 2)
										+ "px",
								"margin-left" : -parseInt(targetWidth / 2)
										+ "px"
							});
							target.show();
							$scope.editCaseId = x.caseid;
							$scope.sortEdit = x.sortid;
							$scope.re_sort_funcEdit = $scope
									.search_re_s_f($scope.sortEdit);
							$scope.funcEdit = x.functionid;
							$scope.funchildnameEdit = x.funchildname;
							$scope.stepEdit = x.step;
							$scope.expectresultEdit = x.expectresult;
							$scope.remarkEdit = x.remark;
						}
						// 关闭添加的弹出框
						$scope.pupuClose = function() {
							$("#spm").hide();
							$(".Popup").hide(1000);
						}
						$scope.pupuEditClose = function() {
							$("#spm").hide();
							$(".PopupEdit").hide(1000);
						}
						// 多选然后标识
						var signGroup = [];
						// 记录整条信息，便于删除后直接从js上删除该信息。
						var signGroupAll = [];
						$scope.isSelected = function(currentrow, isCheck) {
							if (isCheck == true) {
								signGroupAll.push(currentrow);
								signGroup.push(currentrow.caseid);
							} else {
								signGroupAll.removeByValue(currentrow);
								signGroup.removeByValue(currentrow.caseid);
							}
							return currentrow;
						}
						// 鼠标点击事件
						$scope.show = function(x) {
							/* toaster.pop('success', "提示", x); */
							toaster.pop({
								type : 'success',
								title : '提示',
								body : x,
								timeout : 1000
							});
						};
						// 提交审核
						$scope.askExamine = function(x, y) {
							if (y == '提审') {
								$http(
										{
											method : 'POST',
											url : "testCase/examineCase.do",
											headers : {
												'Content-Type' : 'application/x-www-form-urlencoded'
											},
											params : {
												examineCaseId : x.caseid,
											},
										}).success(function(data) {// 这里的data，就是后台传递过来的数据
									if (data.tag == "success") {
										toaster.pop({
											type : 'success',
											title : '提示',
											body : "提交审核成功",
											timeout : 2000
										});
										index = $scope.testCases.indexOf(x);
										x.examine = '审核中';
										// 替换
										var z = x;
										$scope.testCases.splice(index, 1, z);
									} else {
										toaster.pop({
											type : 'error',
											title : '提示',
											body : "提交审核失败",
											timeout : 2000
										});

									}
								})
										.error(
												function(data, status, headers,
														config) {
													toaster.pop({
														type : 'error',
														title : '提示',
														body : "请求出错了",
														timeout : 2000
													});

												});
							}

						}
						// 显示是提审还是待审还是通过
						$scope.isExamineBtn = function(x) {

							if (x.examine == "审核通过") {
								$scope.isShowExamine = false;
								return "提审";
							} else if (x.examine == "待审核") {
								$scope.isShowExamine = true;
								return "提审";
							} else if (x.examine == "未审核通过") {
								$scope.isShowExamine = true;
								return "提审";
							} else {
								$scope.isShowExamine = false;
							}
						}
						// 主添加方法
						$scope.addCase = function() {
							if ($scope.sortAdd == undefined
									|| $scope.sortAdd == null) {
								toaster.pop({
									type : 'error',
									title : '提示',
									body : "用例分类不能为空",
									timeout : 2000
								});
							} else if ($scope.funcAdd == undefined
									|| $scope.funcAdd == null) {
								toaster.pop({
									type : 'error',
									title : '提示',
									body : "功能概括不能为空",
									timeout : 2000
								});
							} else if ($scope.funchildname == undefined
									|| $scope.funchildname == null
									|| $scope.funchildname.trim() == '') {
								toaster.pop({
									type : 'error',
									title : '提示',
									body : "功能名称不能为空",
									timeout : 2000
								});
							} else if ($scope.step == undefined
									|| $scope.step == null
									|| $scope.step.trim() == '') {
								toaster.pop({
									type : 'error',
									title : '提示',
									body : "操作步骤不能为空",
									timeout : 2000
								});
							} else if ($scope.expectresult == undefined
									|| $scope.expectresult == null
									|| $scope.expectresult.trim() == '') {
								toaster.pop({
									type : 'error',
									title : '提示',
									body : "预期结果不能为空",
									timeout : 2000
								});
							} else {
								$http(
										{
											method : 'POST',
											url : "testCase/insertCase.do",
											headers : {
												'Content-Type' : 'application/x-www-form-urlencoded'
											},
											params : {
												sortAdd : $scope.sortAdd,
												funcAdd : $scope.funcAdd,
												funchildname : encodeURI($scope.funchildname),
												step : encodeURI($scope.step),
												expectresult : encodeURI($scope.expectresult),
												remark : $scope.remark == undefined ? ""
														: encodeURI($scope.remark)
											},
										}).success(function(data) {// 这里的data，就是后台传递过来的数据
									if (data.tag == "success") {
										toaster.pop({
											type : 'success',
											title : '提示',
											body : "添加用例成功",
											timeout : 2000
										});
										$scope.funchildname = '';
										$scope.step = '';
										$scope.expectresult = '';
										$scope.remark = '';
									} else {
										toaster.pop({
											type : 'error',
											title : '提示',
											body : "添加用例失败",
											timeout : 2000
										});

									}
								})
										.error(
												function(data, status, headers,
														config) {
													toaster.pop({
														type : 'error',
														title : '提示',
														body : "请求出错了",
														timeout : 2000
													});

												});
							}
						}
						// 主查询方法
						$scope.selectCase = function() {
							/*
							 * alert("sortid=" + $scope.sort + "functionid" +
							 * $scope.func + "审核状态" + examineGroup + "用例归属：" +
							 * $scope.caseAscr + "功能名称：" + $scope.selectFunction +
							 * "关键字：" + $scope.selectKeyword);
							 */
							var sort = $scope.sort == undefined ? null
									: $scope.sort;
							var func = $scope.func == undefined ? null
									: $scope.func;
							var caseAscr = $scope.caseAscr == undefined ? null
									: $scope.caseAscr;
							var selectFunction = encodeURI($scope.selectFunction == undefined ? null
									: $scope.selectFunction)
							var selectKeyword = encodeURI($scope.selectKeyword == undefined ? null
									: $scope.selectKeyword);
							/*
							 * alert("sortid=" + sort + "functionid" + func +
							 * "审核状态" + examineGroup + "用例归属：" + caseAscr +
							 * "功能名称：" + selectFunction + "关键字：" +
							 * selectKeyword);
							 */
							$http(
									{
										method : 'GET',
										url : "testCase/selectCase.do",
										headers : {
											'Content-Type' : 'application/x-www-form-urlencoded'
										},
										params : {
											sort : sort,
											func : func,
											examineGroup : encodeURI(examineGroup),
											caseAscr : caseAscr,
											selectFunction : selectFunction,
											selectKeyword : selectKeyword
										},
									})
									.success(
											function(data) {// 这里的data，就是后台传递过来的数据
												if (data.tag == "success") {
													for (i = 0; i < data.result.length; i++) {
														var newDate = new Date();
														newDate
																.setTime(data.result[i].addtime);
														data.result[i].addtime = newDate
																.format('yyyy-MM-dd h:m:s');
														switch (data.result[i].ispublic) {
														case 0:
															data.result[i].ispublic = "公共";
															break;
														case 1:
															data.result[i].ispublic = "个人";
															break;
														default:
															break;
														}
														switch (data.result[i].examine) {
														case 0:
															data.result[i].examine = "审核中";
															break;
														case 1:
															data.result[i].examine = "审核通过";
															break;
														case 2:
															data.result[i].examine = "待审核";
															break;
														case 3:
															data.result[i].examine = "未审核通过";
															break;
														default:
															break;
														}
													}
													$scope.testCasesAll = data.result;

													// 计算结果总数
													$scope.resultAllLength = $scope.testCasesAll.length;
													// 计算总页码
													$scope.pageCount = Math
															.ceil($scope.resultAllLength / 10);
													// 本页面的记录
													$scope.testCases = [];
													for (var a = 0; a < 10; a++) {
														if (a < $scope.resultAllLength) {
															$scope.testCases
																	.push($scope.testCasesAll[a]);
														}
													}
													$scope.isShowPage = true;
													// 总页码加入到数组里面
													$scope.sub(1);
													$scope.p_current = 1;
												} else {
													$scope.resultAllLength = 0;
													toaster.pop({
														type : 'error',
														title : '提示',
														body : "查询结果为空",
														timeout : 2000
													});
													$scope.testCases = [];
												}
												$scope
														.beginAndEnd($scope.p_current);
											}).error(
											function(data, status, headers,
													config) {
												toaster.pop({
													type : 'error',
													title : '提示',
													body : "请求出错了",
													timeout : 2000
												});

											});
						}
						// 分页算法
						$scope.calculateIndexes = function(x) {
							var indexes = [];
							for (var i = 1; i <= x; i++) {
								indexes.push(i);
							}
							return indexes;
						};
						// 计算第一条和最后一条是多少？
						$scope.beginAndEnd = function(p_current) {
							$scope.start = (p_current - 1) * 10 + 1;
							$scope.end = (p_current) * 10;
							if ($scope.resultAllLength < 1) {
								$scope.resultAllLength = 0;
								$scope.start = 0;
								$scope.end = 0;
							}
							if ($scope.end > $scope.resultAllLength) {
								$scope.end = $scope.resultAllLength;
							}
						}

						// 审核状态
						var examineGroup = [];
						$scope.examine = function(x, isChecked) {
							if (isChecked == true) {
								examineGroup.push(x);
							} else {
								examineGroup.removeByValue(x);
							}
						}
						// 测试方法
						$scope.test = function() {
							alert($scope.pass);
						};
						// raido点击方法
						$scope.radioClick = function(x) {
							$scope.caseAscr = x.target.value;
						};
						// 默认radio选中
						$scope.caseAscr = 'all';
						// sortchange,选择功能列表
						$scope.sortChange = function(x) {
							if (x == 'select') {
								if ($scope.sort == 0) {
									$scope.re_sort_func = null;
								} else {
									$scope.re_sort_func = $scope
											.search_re_s_f($scope.sort);
								}
							} else if (x == 'add') {
								if ($scope.sortAdd == 0) {
									$scope.re_sort_funcAdd = null;
								} else {
									$scope.re_sort_funcAdd = $scope
											.search_re_s_f($scope.sortAdd);
								}
							} else {
								if ($scope.sortEdit == 0) {
									$scope.re_sort_funcEdit = null;
								} else {
									$scope.re_sort_funcEdit = $scope
											.search_re_s_f($scope.sortEdit);
								}

							}
						}
						// functionchange,选择功能分类
						$scope.functionChange = function() {
							// alert($scope.func);
						}
						$scope.search_re_s_f = function(x) {
							var re_sort_func = [];
							for (var i = 0; i < $scope.sort_func.length; i++) {
								if ($scope.sort_func[i].sortid == x) {
									re_sort_func.push($scope.sort_func[i]);
								}
							}
							return re_sort_func;
						};
						// 获取测试用例分类
						initsorts($scope, $http);
					})
	// 添加测试用例,弹出框,开启
	popupLayer("btn_add", "Popup");

})
function setPage(pageCount, num, first) {// 创建保存页码数组的函数
	// first第一页的页码
	var pages = []; // 创建分页数组
	var page = pageCount;
	if (page <= num) {
		for (var i = 1; i <= page; i++) {
			pages.push(i);
		}
	}
	if (page > num) {
		for (var i = first; i < first + num; i++) {
			pages.push(i);
		}
	}
	return pages;
}
function initsorts($scope, $http) {
	$http({
		method : 'GET',
		url : "testCase/getSorts.do",
		headers : {
			'Content-Type' : 'application/x-www-form-urlencoded'
		},
	}).success(function(data) {// 这里的data，就是后台传递过来的数据
		if (data.tag == "success") {
			$scope.sorts = data.sortList;
			// 测试用例分类默认值
			$scope.sort = $scope.sorts[0].sortid;
			$scope.sort_func = data.functionList;
			// $scope.func = $scope.functionList[0].functionid;
		} else {
			toaster.pop({
				type : 'error',
				title : '提示',
				body : "查询失败",
				timeout : 2000
			});
		}
	}).error(function(data, status, headers, config) {
		toaster.pop({
			type : 'error',
			title : '提示',
			body : "请求出错了",
			timeout : 2000
		});

	});
}

String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
};
Array.prototype.removeByValue = function(val) {
	for (var i = 0; i < this.length; i++) {
		if (this[i] == val) {
			this.splice(i, 1);
			break;
		}
	}
};
Date.prototype.format = function(format) {
	var date = {
		"M+" : extra(this.getMonth() + 1),
		"d+" : extra(this.getDate()),
		"h+" : extra(this.getHours()),
		"m+" : extra(this.getMinutes()),
		"s+" : extra(this.getSeconds()),
		"q+" : extra(Math.floor((this.getMonth() + 3) / 3)),
		"S+" : extra(this.getMilliseconds())
	};
	if (/(y+)/i.test(format)) {
		format = format.replace(RegExp.$1, (this.getFullYear() + '')
				.substr(4 - RegExp.$1.length));
	}
	for ( var k in date) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? date[k]
					: ("00" + date[k]).substr(("" + date[k]).length));
		}
	}
	return format;
};
// 补位函数。
function extra(x) {
	// 如果传入数字小于10，数字前补一位0。
	if (x < 10) {
		return "0" + x;
	} else {
		return x;
	}
}
function indexOf(arr, item) {
	if (Array.protopy.indexOf) {
		return arr.indexOf(item);
	} else {
		for (var i = 0; i < arr.length; i++) {
			if (arr[i] === item)
				return i;
			else
				return -1;
		}
	}
}